package entity

import (
	"time"

	"github.com/google/uuid"
	"gorm.io/gorm"
)

type History struct {
	ID         string         `json:"id" gorm:"column:id"`
	Num        int            `json:"num" gorm:"num"`
	WorkflowID string         `json:"workflow_id" gorm:"column:workflow_id"`
	TriggerID  string         `json:"trigger_id" gorm:"column:trigger_id"`
	DSL        string         `json:"dsl" gorm:"column:dsl"`
	CreatedAt  time.Time      `json:"created_at" gorm:"created_at"`
	Status     string         `json:"status" gorm:"column:status"`
	Nodes      []HistoryNode  `json:"nodes" gorm:"-"`
	Trigger    HisotryTrigger `json:"trigger" gorm:"-"`
}

func (h *History) TableName() string {
	return "history"
}

func (h *History) BeforeCreate(tx *gorm.DB) (err error) {
	h.ID = uuid.NewString()
	h.Status = "NotCreated"
	return
}

type HistoryNode struct {
	ID        string `json:"id" gorm:"column:id"`
	HistoryID string `json:"history_id" gorm:"column:history_id"`
	Name      string `json:"name" gorm:"column:name"`
	Script    string `json:"script" gorm:"column:script"`
	Status    string `json:"status" grom:"status"`
	Step      int    `json:"step" gorm:"step"`
	Stdout    string `json:"stdout" gorm:"stdout"`
	Stderr    string `json:"stderr" gorm:stderr`
	Ret       int    `json:"ret" gorm:"ret"`
}

func (h *HistoryNode) TableName() string {
	return "history_node"
}

func (h *HistoryNode) BeforeCreate(tx *gorm.DB) (err error) {
	h.ID = uuid.NewString()
	h.Status = "NotCreated"
	return
}

func (h *HistoryNode) UpdateStatus(status string) (err error) {
	h.Status = status
	return nil
}
